<template>
  <el-dialog width="60%" :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
        <el-col :span="12">
          <!-- 预警项编码 -->
          <el-form-item label="预警项编码" prop="eventCode">
            <!--下拉框-->
            <el-select v-model="dataForm.eventCode" placeholder="预警项编码" clearable>
              <el-option v-for="item in this.eventList" :key="item.eventCode" :label="item.eventName" :value="item.eventCode"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 业务类别（系统字典） -->
          <el-form-item label="业务类别" prop="bizSort">
            <!--下拉框-->
            <el-select v-model="dataForm.bizSort" placeholder="业务类别" clearable>
              <el-option v-for="template in $getDictList('MSG_BIZ_SORT', true)" :key="template.val" :label="template.label"
                :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 业务类型（系统字典） -->
          <el-form-item label="业务类型" prop="bizType">
            <!--下拉框-->
            <el-select v-model="dataForm.bizType" placeholder="业务类型" clearable>
              <el-option v-for="template in $getDictList('MSG_BIZ_TYPE1', true)" :key="template.val" :label="template.label"
                :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <!-- 推送类型（1循环，0立即） -->
          <el-form-item label="推送类型" prop="pushType">
            <!--下拉框-->
            <el-select v-model="dataForm.pushType" placeholder="推送类型" clearable>
              <el-option v-for="template in $getDictList('MSG_PUSH_TYPE', true)" :key="template.val" :label="template.label"
                :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 临界点 -->
          <el-form-item label="临界点" prop="timePoint">
            <!--日期框-->
            <el-date-picker v-model="dataForm.timePoint" value-format="yyyy-MM-dd HH:mm:ss" placeholder="临界点" type="date"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 冗余字段 -->
          <el-form-item label="业务单号" prop="refCode">
            <!--文本框-->
            <el-input v-model="dataForm.refCode" placeholder="业务单号" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 内容-消息参数 -->
          <el-form-item label="消息参数" prop="content">
            <!--文本域-->
            <el-input type="textarea" :rows="3" v-model="dataForm.content" placeholder="消息参数"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 关联业务id - 用于有原始数据 -->
          <el-form-item label="Long外键" prop="bizId">
            <!--文本框-->
            <el-input v-model="dataForm.bizId" placeholder="关联业务id" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 关联字段 - 用于没有原始数据 -->
          <el-form-item label="String外键" prop="refId">
            <!--文本框-->
            <el-input v-model="dataForm.refId" placeholder="关联字段" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      eventList: [],
      dataForm: {
        id: '',
        eventCode: '',
        bizSort: '',
        bizType: '',
        content: '',
        timePoint: '',
        pushType: '',
        bizId: '',
        refId: '',
        refCode: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
      }
    }
  },
  created () {
    this.loadEventList()
  },
  methods: {
    loadEventList () {
      this.$http.get('/msg/msgBizEvent/list', { params: { flag: 1 } }).then(({ data: res }) => {
        this.eventList = res.data
      })
    },
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/msg/msgBizContent/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/msg/msgBizContent/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
